package cn.good.yan.w3;

import cn.good.yan.w3.component.LoginHandlerInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvcConfigurer
 *      所有定制 - Web功能，使用【WebMvcConfigurer】
 * @author shengren.yan
 * @create 2023-10-03
 */
@Configuration
public class SpringMvcConfiguer implements WebMvcConfigurer {

    /** 默认显示首页 */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 显示首页 的配置   前端页在： /resources/templates/每个页面
        registry.addViewController("/").setViewName("main/index");
        registry.addViewController("/index.html").setViewName("main/login");
        registry.addViewController("/home").setViewName("main/index");
        registry.addViewController("/login.html").setViewName("main/login");
        registry.addViewController("/login").setViewName("main/login");
    }

    /** 拦截器 */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor())
                //指定要拦截的请求 /** 表示拦截所有请求
                .addPathPatterns("/**")
                //排除不需要拦截的请求路径
                .excludePathPatterns("/login","/login.html","/doLogin")
                //springboot2+之后需要将静态资源文件的访问路径 也排除
                .excludePathPatterns("/css/**","/img/**","/js/**");
                // .excludePathPatterns("/home/**");
    }


}
